from Dev_state import Dev
from Connection import Json_encoder
from Connection import Json_decoder
import socket,json
import traceback

data = {}

some = Dev("Ouyang","on").getDeviceInformation()
data['Ouyang'] = some

class encode(json.JSONEncoder):
    def default(self,obj):
        d = {}
        d['__class__'] = obj.__class__.__name__
        d['__module__'] = obj.__module__
        d.update(obj.__dict__)
        return d


try:
    sock  = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect(("127.0.0.1",50001))
except socket.error,e :
    print "error : %s" %e
some2 = Json_encoder().encode(some)
print some2
print "send ....",
sock.sendall(some2)
print "ok"
print "get...",
get =  sock.recv(4096)
print "ok"
try:
    get2 = Json_decoder().decode(get)
except ValueError:
    print "this is not json"
print get

choice = raw_input("input > ")

if choice == "stop":
    sock.close()
    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect(("127.0.0.1",50001))
    sock.sendall(choice)
    try:
        sock.close()
        print "exit"
    except :
        traceback.print_exc()


